@page "{Id}"
@model EditModel

@using System.ComponentModel.DataAnnotations;
@using Microsoft.AspNetCore.Mvc.RazorPages
@using OrchardCore.ContentManagement;
@using OrchardCore.ContentManagement.Display;
@using OrchardCore.DisplayManagement.ModelBinding;
@using YesSql;

@functions
{
    public class EditModel : PageModel
    {
        private readonly IContentManager _contentManager;
        private readonly IContentItemDisplayManager _contentDisplay;
        private readonly IUpdateModelAccessor _updateModelAccessor;
        private readonly ISession _session;

        public EditModel(
            IContentManager contentManager,
            IContentItemDisplayManager contentDisplay,
            IUpdateModelAccessor updateModelAccessor,
            ISession session)
        {
            _contentManager = contentManager;
            _contentDisplay = contentDisplay;
            _updateModelAccessor = updateModelAccessor;
            _session = session;
        }

        [BindProperty(SupportsGet = true)]
        public string Id { get; set; }

        [Required]
        [BindProperty]
        public string Text { get; set; }

        public string Title { get; set; }
        public string Submit { get; set; }

        public async Task<ActionResult> OnGetAsync()
        {
            var contentItem = await _contentManager.GetAsync(Id);

            if (contentItem == null)
            {
                return NotFound();
            }

            Title = contentItem.DisplayText ?? "Foo Title";
            Text = contentItem.Content.TestContentPartA.Line;

            return Page();
        }

        public async Task<IActionResult> OnPostDelete()
        {
            var contentItem = await _contentManager.GetAsync(Id);
            if (contentItem == null)
            {
                return NotFound();
            }
            await _contentManager.RemoveAsync(contentItem);

            return RedirectToPage("/Foo/List");
        }

        public async Task<IActionResult> OnPostUpdate()
        {
            var contentItem = await _contentManager.GetAsync(Id);

            if (contentItem == null)
            {
                return NotFound();
            }

            var updater = _updateModelAccessor.ModelUpdater;
            var shape = await _contentDisplay.UpdateEditorAsync(contentItem, updater, false);

            if (!ModelState.IsValid)
            {
                _session.Cancel();
                return Page();
            }

            contentItem.Content.TestContentPartA.Line = Text;
            _session.Save(contentItem);

            return RedirectToPage("/Foo/List");
        }
    }
}

@await Html.PartialAsync("Header", Model)

<h2>Hello from @ViewContext.RouteData.Values["page"]</h2>

<form asp-route-id="@Model.Id" method="post">
    <button asp-page-handler="Delete" class="btn btn-sm btn-danger float-right mx-1">Delete</button>
    <input asp-for="Text" />
    <button asp-page-handler="Update" class="btn btn-sm btn-warning float-right mx-1">Update</button>
</form>
